import util from '../../util/index'
let rid;
let rType;
/**
 * 预约弹窗 （已废弃）
 */
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    visible: false,
    selected: -1,
    data: [],
    title:"选择站点",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    show(id, type = 1, data = []) {
      rid = id;
      rType = type;
      let title = "选择站点";
      if (type == 1) {
        data = data.slice(0,-1);
        title = "选择上车站点";
      } else if (type == 2) {
        data = data.slice(1);
        title = "选择下车站点";
      }
      this.setData({
        selected: -1,
        visible: true,
        data,
        title,
      })
      // util.ajax("order", {
      //   data: {
      //     action: "getAppointmenDate",
      //     id
      //   }
      // }).then(({
      //   code,
      //   data,
      //   msg
      // }) => {
      //   wx.hideLoading()
      //   if (code == 1) {
      //     this.setData({
      //       data,
      //       visible: true,
      //     })
      //   } else {
      //     util.tip(msg);
      //   }
      // }, wx.hideLoading)
      // this.setData({
      //   visible:true,
      // })
    },
    confirm() {
      const {
        selected,
        data
      } = this.data;
      if (selected == -1) {
        util.tip("请选择站点")
        return;
      }
      this.hide();
      wx.showLoading({
        title: '操作中...',
      })
      const {
        name
      } = data[selected]
      util.ajax("order", {
        data: {
          action: "book",
          name,
          rid,
        }
      }).then(({
        code,
        msg
      }) => {
        wx.hideLoading();
        if (code == 1) {
          util.tip("预约成功", "success");
          let routeId = wx.getStorageSync("routeId");
          if (routeId) {
            routeId[rType] = rid;
          } else {
            routeId = {
              [rType]: rid
            };
          }
          wx.setStorageSync("routeId", routeId)
        } else if(code == -1){
          wx.navigateTo({
            url: '/pages/login/login',
          }) 
        }else if(code == -3){
          wx.navigateTo({
            url: '/pages/improve/improve',
          })
        }else {
          util.tip(msg);
        }
        }, wx.hideLoading)

    },
    cannel() {
      this.hide();
    },
    hide() {
      this.setData({
        visible: false,
      })
    },
    select({
      target: {
        dataset: {
          index
        }
      }
    }) {
      // if (this.data.data[index].status == 1) {
      this.setData({
        selected: index
      })
      // }

    },
  }
})